import { Global, Injectable, Logger, NestMiddleware } from '@nestjs/common';
import  dayjs from 'dayjs';
import { Request, Response, NextFunction } from 'express';

@Injectable()
export class LoggerMiddleware implements NestMiddleware {
  private logger = new Logger('TimingMiddleware');
  
  use(req: Request, res: Response, next: NextFunction) {
    const start = Date.now();
    res.on('close',(val)=>{
      const duration = Date.now() - start;
      console.log(`[NYF] -${dayjs().format("YYYY-MM-DD HH:mm:ss")} -path ${req.path} method ${req.method} ${res.statusCode} ${duration}ms`)
      // console.log('close',val) 
    })
    res.on('finish', () => {
      // console.log(duration+'ms',req.method,req.path)
    });

    next();
  }
}